﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ZoneInZone.CCoZZZoneInZoneCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ZoneInZone;

public class CCoZZZoneInZoneCmd : TxToggleButtonCommand
{
  private bool _isToggled;
  private TxCommandPrivateDataStorage _dataStorage;
  private CApZZZoneInZoneApp _app;
  private CApZZZoneInZoneEnabler _enabler;

  public CCoZZZoneInZoneCmd()
  {
    this._enabler = new CApZZZoneInZoneEnabler();
    this._dataStorage = TxCommandPrivateDataStorage.CreateInstanceFromCommandType<CCoZZZoneInZoneCmd>();
    this._app = new CApZZZoneInZoneApp();
    this.LoadState();
    if (!this._isToggled)
      return;
    this.ListenToEvents();
    this._app.ShowZonePreviewIfAvailable();
  }

  public virtual void Execute(object cmdParams)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    this._isToggled = !this._isToggled;
    this.SaveState();
    if (this._isToggled)
    {
      this._app.ShowZonePreviewIfAvailable();
      this.ListenToEvents();
    }
    else
    {
      this._app.HideZonePreviewIfAvailable();
      this.StopListeningToEvents();
    }
    Cursor.Current = current;
  }

  public virtual string Name => DnProcessSimulateCommands.ZoneInZone.Resources.StringTable.ZONE_IN_ZONE_CMD_NAME;

  public virtual string Category => DnProcessSimulateCommands.ZoneInZone.Resources.StringTable.ZONE_IN_ZONE_CMD_CATEGORY;

  public virtual string Tooltip => DnProcessSimulateCommands.ZoneInZone.Resources.StringTable.ZONE_IN_ZONE_CMD_TOOLTIP;

  public virtual string Description => DnProcessSimulateCommands.ZoneInZone.Resources.StringTable.ZONE_IN_ZONE_CMD_DESCRIPTION;

  public virtual string Bitmap => "ZoneInZone.Resources.ZoneInZone.bmp";

  public virtual string LargeBitmap => "ZoneInZone.Resources.ZoneInZoneLarge.png";

  public virtual bool Connect() => ((TxButtonCommand) this).Connect();

  public virtual void Disconnect() => ((TxButtonCommand) this).Disconnect();

  public bool IsToggled
  {
    get => this._isToggled;
    set => this._isToggled = value;
  }

  public virtual bool Push => this._isToggled;

  public virtual ITxCommandEnabler CommandEnabler => (ITxCommandEnabler) this._enabler;

  private void ListenToEvents()
  {
    TxApplication.ActiveSelection.Changed += new TxSelection_ChangedEventHandler(this.ActiveSelection_Changed);
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.ActiveDocument_Unloading);
  }

  private void StopListeningToEvents()
  {
    TxApplication.ActiveSelection.Changed -= new TxSelection_ChangedEventHandler(this.ActiveSelection_Changed);
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.ActiveDocument_Unloading);
  }

  private void ActiveSelection_Changed(object sender, EventArgs args)
  {
    if (this._app.IsSelectionChangedInternally)
      return;
    this._app.ShowZonePreviewIfAvailable();
    this._app.IsSelectionChangedInternally = true;
    this._app.SynchronizeSelectionBetweenLocationsAndZones();
    this._app.IsSelectionChangedInternally = false;
  }

  private void ActiveDocument_Unloading(object sender, TxDocument_UnloadingEventArgs args)
  {
    this._app.HideZonePreviewIfAvailable();
  }

  private void LoadState()
  {
    int num;
    if (!this._dataStorage.TryGetIntValue(((object) this).GetType().Name, ref num))
      return;
    this._isToggled = num == 1;
  }

  private void SaveState()
  {
    this._dataStorage.SetIntValue(((object) this).GetType().Name, this._isToggled ? 1 : 0);
  }
}
